<template>
  <div class="public-weChatOfficialAccountConfiguration-set gray-box top-redius border-bottom-gray">
    <div class="title">
      {{ $t('sysManagement.weChatOfficialAccountConfiguration') }}
    </div>
    <el-form
      ref="dataFormRef"
      label-width="240px"
      class="set-form"
      label-position="left"
      :rules="dataRule"
      :model="dataForm"
      @submit.prevent
      @keyup.enter="onSubmit(dataFormRef)"
    >
      <el-form-item
        :label="`${$t('sysManagement.weChatOfficialAccount')}appId:`"
        style="width:640px"
        prop="appId"
      >
        <el-input
          v-model="dataForm.appId"
          :placeholder="`${$t('sysManagement.weChatOfficialAccount')}appId`"
          controls-position="right"
        />
      </el-form-item>
      <el-form-item
        :label="`${$t('sysManagement.weChatOfficialAccount')}secret:`"
        style="width:640px"
        prop="secret"
      >
        <el-input
          v-model="dataForm.secret"
          :placeholder="`${$t('sysManagement.weChatOfficialAccount')}secret`"
          controls-position="right"
        />
      </el-form-item>
      <div
        class="default-btn"
        @click="onSubmit(dataFormRef)"
      >
        {{ $t('sysManagement.save') }}
      </div>
    </el-form>
  </div>
</template>

<script setup>
import { ElMessage } from 'element-plus'

const emit = defineEmits(['refreshDataList'])
const dataFormRef = ref(null)
const dataForm = ref({
  appId: null,
  secret: null,
  token: null,
  aesKey: null
})
const dataRule = {
  appId: [
    { required: true, message: `${$t('sysManagement.weChatOfficialAccount')} appId ${$t('sysManagement.nullTips')}`, trigger: 'blur' }
  ],
  secret: [
    { required: true, message: `${$t('sysManagement.weChatOfficialAccount')} secret ${$t('sysManagement.nullTips')}`, trigger: 'blur' }
  ]
}

onMounted(() => {
  init()
})

const init = () => {
  nextTick(() => {
    http({
      url: http.adornUrl('/sys/pconfig/info/' + 'MP_CONFIG'),
      method: 'get',
      params: http.adornParams({
        id: dataForm.value.id,
        key: dataForm.value.paramKey
      })
    }).then(({ data }) => {
      if (data) {
        dataForm.value = JSON.parse(data)
      }
    })
    dataFormRef.value?.resetFields()
  })
}
// 表单提交
const onSubmit = async formEl => {
  const obj = {}
  obj.appId = dataForm.value.appId
  obj.secret = dataForm.value.secret
  if (!formEl) return
  await formEl.validate(valid => {
    if (!valid) return
    http({
      url: http.adornUrl('/sys/pconfig/save'),
      method: 'post',
      data: http.adornData({
        paramKey: 'MP_CONFIG',
        paramValue: JSON.stringify(obj),
        remark: $t('sysManagement.weChatOfficialAccountConfiguration')
      })
    }).then(({ data }) => {
      if (data) init()
      ElMessage({
        message: $t('publics.operation'),
        type: 'success',
        duration: 1500,
        onClose: () => {
          emit('refreshDataList')
        }
      })
    })
  })
}
</script>

<style lang="scss" scoped>
@use '../index.scss';
</style>
